package com.xms.sdk.client.aggroot;

import com.xms.core.exception.XmsException;
import com.xms.form.service.ISystemFormFinderService;
import com.xms.queryview.service.IQueryViewFinderService;
import com.xms.schema.service.relationship.IRelationShipFinderService;
import com.xms.sdk.client.DataProviderBase;
import com.xms.sdk.core.query.QueryBase;
import com.xms.sdk.data.IOrganizationDataRetriever;
import com.xms.sdk.service.IDataFinder;
import com.xms.sdk.service.IFetchDataService;
import com.xms.security.identity.ICurrentUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * AggFinder
 * </p>
 *
 * @author migo
 * @since 2021/9/8
 */
@Service
public class AggFinder extends DataProviderBase implements IAggFinder{
    private ISystemFormFinderService _systemFormFinderService;
    private IOrganizationDataRetriever _organizationDataRetriever;
    private IDataFinder _dataFinder;
    private IRelationShipFinderService _relationShipFinderService;
    private IQueryViewFinderService _queryViewFinderService;
    private IFetchDataService _fetchDataService;
    private AggregateRoot _aggregateRoot = new AggregateRoot();
    public ICurrentUser User;
    @Autowired
    public void AggFinder(ISystemFormFinderService systemFormFinderService,
                          IOrganizationDataRetriever organizationDataRetriever,
                          IDataFinder dataFinder,
                          IRelationShipFinderService relationShipFinderService,
                          IQueryViewFinderService queryViewFinderService,
                          IFetchDataService fetchDataService,
                          ICurrentUser currentUser) {
        _systemFormFinderService = systemFormFinderService;
        _organizationDataRetriever = organizationDataRetriever;
        _dataFinder = dataFinder;
        _relationShipFinderService = relationShipFinderService;
        _queryViewFinderService = queryViewFinderService;
        _fetchDataService = fetchDataService;
        _currentUser = currentUser;
    }

    @Override
    public AggregateRoot retrieve(QueryBase request, boolean ignorePermissions) throws XmsException {
        /*String entityname = "";
        UUID recordId = null;
        UUID formId = null;

        _aggregateRoot.MainEntity = _dataFinder.retrieveById(entityname, recordId);

        //表单列表
        Systemform formEntity = null;
        formEntity = _systemFormFinderService.getById(formId);
        FormBuilder formBuilder = new FormBuilder(formEntity.FormConfig);

        List<PanelDescriptor> panelDescriptors = formBuilder.Form.Panels;

        foreach (var panel in panelDescriptors)
        {
            foreach (var section in panel.Sections)
            {
                foreach (var row in section.Rows)
                {
                    foreach (var cell in row.Cells)
                    {
                        if (cell.Control.ControlType == FormControlType.SubGrid)
                        {
                            var param = (SubGridParameters)cell.Control.Parameters;

                            //param.ViewId;
                            //param.RelationshipName

                            var queryView = _queryViewFinder.FindById(Guid.Parse(param.ViewId));
                            if (queryView != null)
                            {
                                //if (!queryView.IsDefault && queryView.IsAuthorization)
                                {
                                }

                                FetchDescriptor fetch = new FetchDescriptor
                                {
                                    //Page = model.Page,
                                    //PageSize = model.PageSize,
                                    //FetchConfig = queryView.FetchConfig,
                                    //GetAll = !model.PagingEnabled
                                };

                                //排序，过滤
                                var relationship = _relationShipFinder.FindByName(param.RelationshipName);
                                var filter = new FilterExpression();
                                var condition = new ConditionExpression(relationship.ReferencingAttributeName, ConditionOperator.Equal, recordId.Value);
                                filter.AddCondition(condition);
                                fetch.Filter = filter;
                                fetch.User = User;
                                var datas = _fetchService.Execute(fetch);
                                //_aggregateRoot.grids.Add("", datas.Items);
                            }
                        }
                    }
                }
            }
        }*/
        return _aggregateRoot;
    }

    @Override
    public AggregateRoot retrieve(QueryBase request) {
        return null;
        //return this.retrieve(request, false);
    }
}
